#!/usr/local/bin/perl
package FTE;
use strict; use warnings;
no if $] >= 5.018,  warnings => "experimental::smartmatch";
use feature qw/switch/;

my ($ax_ok,$cv_ok,$pd);

sub init_fte
{
    ($ax_ok,$cv_ok,$pd)=@_;
}

sub get_ext
{
    my $e=$_[0];
    $e=~ s/.*\.//;
    return if ( ! defined $e
		|| (length ($e) + 1 == length($_[0])));
    return lc($e);
}

sub is_oke
{
    require "imv_ft.pm";
    my $e=&get_ext($_[0]);
    return unless defined $e;
    if($ax_ok){
	given($e){
	    when("zip")            { return $FT::FT_ZIP   if &FT::is_available($FT::FT_ZIP);}
	    when(["gz","tgz","z"]) { return $FT::FT_GZIP  if &FT::is_available($FT::FT_GZIP);}
	    when("bz2")            { return $FT::FT_BZIP2 if &FT::is_available($FT::FT_BZIP);}
	    when("7z")             { return $FT::FT_7ZIP  if &FT::is_available($FT::FT_7ZIP);}
	    when("xz")             { return $FT::FT_XZ    if &FT::is_available($FT::FT_XZ);}
	    when("rar")            { return $FT::FT_RAR   if &FT::is_available($FT::FT_RAR);}
	}
    }
    if($cv_ok){
	given($e){
	    when("pdf")            { return $FT::FT_PDF if &FT::is_available($FT::FT_PDF);}
	    when("ps")             { return $FT::FT_PS  if &FT::is_available($FT::FT_PS);}
	    when("eps")            { return $FT::FT_EPS if &FT::is_available($FT::FT_EPS);}
	}
    }
    foreach(glob("$pd/*")){
	my $pf=$_;
	$pf=~s/\.[^.]*$//;
	$pf=~s/^$pd\///;
	return $_ if($e=~/$pf/i && -x $_);
    }
    return undef;
}

1;
